package com.hsck.order.kds.main.fragment.order.batch

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.databinding.DynamicBatchOrderingItemLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class DynamicBatchOrderingAdapter(val activity: Activity) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var list: MutableList<TestListBean> = arrayListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemVH(
            DynamicBatchOrderingItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    inner class ItemVH(val binding: DynamicBatchOrderingItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bindData(position: Int) {
            val item = list[position]
            binding.bean = item
            itemView.setOnClickListener {
                item.itemSelected = !item.itemSelected
                onDishOperationListener.onDishOperationSelectedAction()
            }
        }
    }

    /**
     * 当前对象的全选中与取消全选
     */
    fun toggleItemSelected() {
        val hasFalse = list.any { !it.itemSelected }
        for (item in list) {
            item.itemSelected = hasFalse
        }
        onDishOperationListener.onDishOperationSelectedAction()
    }

    fun setListData(newList: MutableList<TestListBean>, append: Boolean = false) {
        if (append) {
            val startPosition = list.size
            list.addAll(newList)
            notifyItemRangeInserted(startPosition, newList.size)
        } else {
            list.clear()
            list.addAll(newList)
            notifyDataSetChanged()
        }
    }

    fun getSelectedList(): List<TestListBean> {
        return list.filter { it.itemSelected }
    }

    fun addItemAfterId(newItem: TestListBean, targetId: String) {
        val targetIndex = list.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            list.add(targetIndex + 1, newItem)
            notifyItemInserted(targetIndex + 1)
        }
    }

    fun removeItemAfterId(targetId: String) {
        val targetIndex = list.indexOfFirst { it.id == targetId }
        if (targetIndex != -1 && targetIndex + 1 < list.size) {
            list.removeAt(targetIndex + 1)
            notifyItemRemoved(targetIndex + 1)
            if (targetIndex < list.size) {
                notifyItemRangeChanged(targetIndex, list.size - targetIndex)
            }
        }
    }

    fun removeItemAfterIndex(targetIndex: Int) {
        if (targetIndex + 1 < list.size) {
            list.removeAt(targetIndex + 1)
            notifyItemRemoved(targetIndex + 1)
            if (targetIndex < list.size) {
                notifyItemRangeChanged(targetIndex, list.size - targetIndex)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ItemVH -> {
                holder.bindData(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    private lateinit var onDishOperationListener: DishOperationListener
    fun setDishOperationListener(onDishOperationListener: DishOperationListener) {
        this.onDishOperationListener = onDishOperationListener
    }

    interface DishOperationListener {
        fun onDishOperationSelectedAction()
    }
}